package ControlLayer;

import java.util.ArrayList;

import DBLayer.DBConnection1;
import DBLayer.DBInvoice;
import DBLayer.DBProductLine;
import DBLayer.DBSalesOrder;
import DBLayer.IFDBInvoice;
import DBLayer.IFDBProductLine;
import DBLayer.IFDBSalesOrder;
import ModelLayer.Customer;
import ModelLayer.Invoice;
import ModelLayer.Product;
import ModelLayer.ProductLine;
import ModelLayer.SalesOrder;

public class OrderCtr {
	
	public OrderCtr()
	{
		
	}
	
	public ArrayList<SalesOrder> findAllOrders()
	{
		IFDBSalesOrder dbsalesOrder = new DBSalesOrder();
	    ArrayList<SalesOrder> allSales = new ArrayList<SalesOrder>();
	    allSales = dbsalesOrder.getAllSalesOrders(false);
	    return allSales;
	}
	
	public ArrayList<SalesOrder> findAllCustomerOrders(int id)
	{
		IFDBSalesOrder dbsalesOrder=new DBSalesOrder();
		ArrayList<SalesOrder> allCustomerOrders= new ArrayList<SalesOrder>();
		allCustomerOrders=dbsalesOrder.getCustomerSalesOrders(id, false);
		return allCustomerOrders;
	}
	
	public SalesOrder findOrderById(int id)
	{
		IFDBSalesOrder dbsalesOrder = new DBSalesOrder();
		SalesOrder order = new SalesOrder();
		order = dbsalesOrder.getSalesOrdersByID(id, false);
		return order;
	}
	
	public double findOrderPrice(int orderId)
	{
		IFDBProductLine dbproductLine= new DBProductLine();
		ArrayList<ProductLine> productLines = new ArrayList<>();
		productLines  = dbproductLine.getProductLinesForOrder(orderId, true);
		double orderPrice=0;
		
		for (ProductLine productLine : productLines) {
			orderPrice=orderPrice+productLine.getSubPrice();
		}
		return orderPrice;
	}
	
	public float findInvoicePrice(int invoiceNo)
	{
		IFDBSalesOrder dbsalesOrder = new DBSalesOrder();
		ArrayList<SalesOrder> salesOrders = new ArrayList<>();
		salesOrders= dbsalesOrder.getSalesOrdersInInvoice(invoiceNo, false);
		float totalPrice =0;
		for (SalesOrder salesOrder : salesOrders) {
			totalPrice+=findOrderPrice(salesOrder.getId());
		}
		return totalPrice;
	}
	
	public ArrayList<ProductLine> findAllProductLinesForOrder(int orderId)
	{
		IFDBProductLine dbpl = new DBProductLine();
		ArrayList<ProductLine> allProductLines = new ArrayList<>();
		allProductLines = dbpl.getProductLinesForOrder(orderId, true);
		return allProductLines;
	}
	
	public Invoice findInvoiceByNo(int number)
	{
		IFDBInvoice dbinvoice = new DBInvoice();
		Invoice invoice = dbinvoice.getInvoiceByNo(number, false);
		return invoice;
	}
	
	public double findProductLinePrice(int orderid, int productId)
	{
		IFDBProductLine dbpl = new DBProductLine();
		ProductLine pl = dbpl.getProductLineByID(productId, orderid, false);
		return pl.getSubPrice();
	}
	
	public void createNewOrder(String orderDate, String deliveryStatus, String deliveryDate, int customerId, 
			int invoiceNo)
	{
		SalesOrder salesOrder= new SalesOrder();
		
		salesOrder.setOrderDate(orderDate);
		salesOrder.setDeliveryStatus(deliveryStatus);
		salesOrder.setDeliveryDate(deliveryDate);
		salesOrder.setCustomer(new Customer(customerId));
		salesOrder.setInvoice(new Invoice(invoiceNo));
		
		try
		{
			DBConnection1.startTransaction();
            IFDBSalesOrder dbSO = new DBSalesOrder();
            dbSO.insertSalesOrder(salesOrder);            
            DBConnection1.commitTransaction();
        }
        catch(Exception e)
        {
            DBConnection1.rollbackTransaction();
        }		
		System.out.println("Order created");
	}
	
	public int updateSalesOrder(int id, String orderDate, String deliveryStatus, String deliveryDate, int customerID, int invoiceNo)
	{
		IFDBSalesOrder dborder = new DBSalesOrder();
		SalesOrder order = new SalesOrder();
		order.setId(id);
		order.setOrderDate(orderDate);
		order.setDeliveryStatus(deliveryStatus);
		order.setDeliveryDate(deliveryDate);
		order.setCustomer(new Customer(customerID));
		order.setInvoice(new Invoice(invoiceNo));
		return dborder.updateSalesOrder(order);
	}
	
	public void createNewInvoice(int invoiceNo, String paymentDate)
	{
		Invoice invoice = new  Invoice();
		
		invoice.setInvoiceNo(invoiceNo);
		invoice.setPaymentDate(paymentDate);
		invoice.setPrice(findInvoicePrice(invoiceNo));
		
		try {
			DBConnection1.startTransaction();
			IFDBInvoice dbinvoice = new DBInvoice();
			dbinvoice.insertInvoice(invoice);
			DBConnection1.commitTransaction();
		} catch (Exception e) {
			DBConnection1.rollbackTransaction();
		}
		System.out.println("Invoice created!");
	}
	
	public void createNewProductLine(int productId, int quantity, float price, int orderId)
	{
		ProductLine pl = new  ProductLine();
		
		pl.setProduct(new Product(productId));
		pl.setQuantity(quantity);
		pl.setSubPrice(price);
		pl.setOrder(new SalesOrder(orderId));
		
		try {
			DBConnection1.startTransaction();
			IFDBProductLine dbpl=new DBProductLine();
			dbpl.insertProductLine(pl);
			DBConnection1.commitTransaction();
		} catch (Exception e) {
			DBConnection1.rollbackTransaction();
		}
		System.out.println("ProductLine created!");
	}
}
